<?php

class Community_Model_MStory
{
    private $_connection=null;
    private $_db=null;
    private $_collection=null;
    private $_function=null;

    public function  __construct()
    {
        $this->_connection=new Mongo();
        $this->_db=$this->_connection->bookstore;
        $this->_collection=$this->_db->story;
        $this->_function=new Default_Model_MFunction();
    }

    public function getStories(&$total, $currentpage=1,$pagesize=10,$lengthtitle=80)
    {
        $total=$this->_collection->find()->count();
        if($total==0)
            return array();
        if(!is_int($currentpage) || !is_int($pagesize))
            return array();
        if($pagesize <=0)
            return array();

        $maxpage=$total/$pagesize;
        if($total%$pagesize!=0)
            $maxpage++;
        if($currentpage<=0)
            $currentpage=1;
        if($currentpage>$maxpage)
            $currentpage=$maxpage;

        $arr= $this->_collection->find()->sort(array("datecomment"=>-1))
                ->skip(($currentpage-1)*$pagesize)->limit($pagesize);
        $story=array();
        $userModel=new Personal_Model_MUser();
        foreach($arr as $aStory)
        {
            $userInfo=$userModel->getInfoBasic($aStory["id_user"]);
            
            $arrDate=$this->_function->stringDateToArray($aStory["date"]);
            $date=$arrDate["day"].'/'.$arrDate["month"].'/'.$arrDate["year"].' '.$arrDate["hour"].':'.$arrDate["minute"].':'.$arrDate["second"];
            $title=$aStory["title"];
            if(strlen($title)>$lengthtitle)
            {
                $pos=strpos($title, " ", $lengthtitle);
                if($pos!=False)
                {
                    $title = substr($title, 0,$pos+1).'...';
                }
            }
            
            $arrTemp=array("_id"=>$aStory["_id"],"id_user"=>$aStory["id_user"],
                "user_image"=>$userInfo["image"],
                "title"=>$aStory["title"],"date"=>$date,
                "view"=>$aStory["view"],"sumcomment"=>$aStory["sumcomment"],);

            array_push($story, $arrTemp);
        }
        return $story;
    }

    public function addStory($uid,$title,$content)
    {
        $userModel=new Personal_Model_MUser();
        if(!$userModel->isExistedUser($uid))
            return false;
        $date=$this->_function->dateToString(Zend_Date::now());
        $query=array("id_user"=>new MongoId($uid),"title"=>$title,"content"=>$content,
                      "date"=>$date,"datecomment"=>$date,"view"=>0,"sumcomment"=>0);
        $this->_collection->insert($query);
        $temp=$this->_collection->findOne($query);
        return $temp["_id"];
    }

    public function getStory($sid)
    {
        $story=$this->_collection->findOne(array("_id"=>new MongoId($sid)));
        if($story!=null)
        {
            $userModel=new Personal_Model_MUser();
            $userInfo=$userModel->getInfoBasic($story["id_user"]);

            $arrDate=$this->_function->stringDateToArray($story["date"]);
            $date=$arrDate["day"].'/'.$arrDate["month"].'/'.$arrDate["year"].' '.$arrDate["hour"].':'.$arrDate["minute"].':'.$arrDate["second"];
            $title=$story["title"];
            if(strlen($title)>80)
            {
                $pos=strpos($title, " ", 80);
                if($pos!=False)
                {
                    $title = substr($title, 0,$pos+1).'...';
                }
            }

            $story["date"]=$date;
            $story["title"]=$title;
            $story["user_name"]=$userInfo["user_name"];
            $story["user_image"]=$userInfo["image"];
            if(isset($story["comment"]) && count($story["comment"])>0)
            {
                for($i=0;$i<count($story["comment"]);$i++)
                {
                    $userIF=$userModel->getInfoBasic($story["comment"][$i]["id_user"]);
                    $story["comment"][$i]["user_name"]=$userIF["user_name"];
                    $story["comment"][$i]["user_image"]=$userIF["image"];
                }

            }
        }
        $this->_collection->update(array("_id"=>new MongoId($sid)),
                                   array('$set'=>array("view"=>((int)$story["view"]+1))));
        return $story;
    }

    public function addComment($uid,$sid,$content)
    {
        $userModel=new Personal_Model_MUser();
        if(!$userModel->isExistedUser($uid))
            return false;
        $story=$this->_collection->findOne(array("_id"=>new MongoId($sid)));
        if($story==null)
            return false;
        if(strlen($this->_function->standardstring($content))==0)
            return false;

        $id=new MongoId($uid);
        $user=new Personal_Model_MUser();
        $userInfo=$user->getInfoBasic($id);
        $date=  Zend_Date::now();
        if(isset($story["comment"]))   // đã có comment
        {
            $sumcomment=(int)($story["sumcomment"]);
            $sumcomment++;
            $order=0;
            foreach($story["comment"] as $comment)
            {
                if((int)$comment["stt"]>$order)
                    $order=(int)$comment["stt"];
            }
            $order++;

            $this->_collection->update($story,array(
                '$push'=>array("comment"=>array("stt"=>$order,"id_user"=>$id,"content"=>$content,"date"=>$date->__toString())),
                '$set'=>array("sumcomment"=>$sumcomment,"datecomment"=>$this->_function->dateToString($date))
                 )
            );
            //$collection->update($abook,array('$set'=>array("sumcomment"=>$sumcomment)));
            //$collection->update($abook,array('$set'=>array("datecomment"=>$date->__toString())));

            return array("stt"=>$order,"id_user"=>$id,"user_name"=>$userInfo["user_name"],
                "user_image"=>$userInfo["image"],"content"=>$content,"date"=>$date->__toString());
        }
        else    // comment đầu tiên
        {
            $this->_collection->update($story,array('$push'=>array(
                "comment"=>array("stt"=>1,"id_user"=>$id,"content"=>$content,"date"=>$date->__toString())
                 ),
                '$set'=>array("datecomment"=>$this->_function->dateToString($date),"sumcomment"=>1)
               )
            );
            //$this->_collection->update($story,array('$set'=>array("sumcomment"=>1)));


            return array("stt"=>1,"id_user"=>$id,"user_name"=>$userInfo["user_name"],
                "user_image"=>$userInfo["image"],"content"=>$content,"date"=>$date->__toString());
        }

        return false;
    }
}



